﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ultima.Xna.Extensions
{
    public static class GraphicsDeviceExtensions
    {
        /// <summary>
        /// Initializes the vertex buffer, index buffer and vertex declaration used by this extension
        /// </summary>
        public static void Initialize(Game game)
        {
            vertexDeclaration = new VertexDeclaration(game.GraphicsDevice, VertexPositionTexture.VertexElements);

            vertices = new VertexPositionTexture[]
                        {
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,0)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,0))
                        };

            indices = new short[] { 0, 1, 2, 2, 3, 0 };
        }

        static VertexDeclaration vertexDeclaration;
        static VertexPositionTexture[] vertices;
        static short[] indices;

        /// <summary>
        /// Draws a quad to the screen at coord Vector2.One * -1 and Vector2.One
        /// </summary>
        public static void DrawQuad(this GraphicsDevice device)
        {
            device.DrawQuad(Vector2.One * -1, Vector2.One);
        }

        /// <summary>
        /// Draws a quad to the screen in the specified coord.
        /// </summary>
        public static void DrawQuad(this GraphicsDevice device, Vector2 v1, Vector2 v2)
        {
            device.VertexDeclaration = vertexDeclaration;

            vertices[0].Position.X = v2.X;
            vertices[0].Position.Y = v1.Y;

            vertices[1].Position.X = v1.X;
            vertices[1].Position.Y = v1.Y;

            vertices[2].Position.X = v1.X;
            vertices[2].Position.Y = v2.Y;

            vertices[3].Position.X = v2.X;
            vertices[3].Position.Y = v2.Y;

            device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);
        }

        /// <summary>
        /// Draws a quad to the screen in the specified coord.
        /// </summary>
        public static void DrawQuad(this GraphicsDevice device, ref Vector2 v1, ref Vector2 v2)
        {
            device.VertexDeclaration = vertexDeclaration;

            vertices[0].Position.X = v2.X;
            vertices[0].Position.Y = v1.Y;

            vertices[1].Position.X = v1.X;
            vertices[1].Position.Y = v1.Y;

            vertices[2].Position.X = v1.X;
            vertices[2].Position.Y = v2.Y;

            vertices[3].Position.X = v2.X;
            vertices[3].Position.Y = v2.Y;

            device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);
        }
    }
}
